package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @ClassName: ProductServiceImpl
 * @Package: com.atguigu.spzx.product.service.impl
 * @Description
 * @Author 孤妤
 * @Date: 2023/12/18 17:11
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    @Transactional(readOnly = true)
    public PageInfo<ProductSku> findProductSkuPage(Integer pageNum, Integer pageSize, ProductSkuDto productSkuDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductSku> productSkuList = productSkuMapper.selectProductSkuPage(productSkuDto);
        PageInfo<ProductSku> productSkuPageInfo = new PageInfo<>(productSkuList);
        return productSkuPageInfo;
    }

    @Override
    @Transactional(readOnly = true)
    public ProductItemVo findProductDetailsById(Long skuId) {
        // 创建返回结果集对象
        ProductItemVo productItemVo = new ProductItemVo();
        // 根据skuId查询商品sku信息并添加到返回结果集对象中
        ProductSku productSku = productSkuMapper.selectProductSkuById(skuId);
        productItemVo.setProductSku(productSku);
        // productId
        Long productId = productSku.getProductId();
        // 根据sku对象productId查询规格
        List<ProductSku> skuList = productSkuMapper.selectProductSkuSpecById(productId);
        Map<String,Object> specMap = new HashMap<>();
        skuList.forEach(item->{
            specMap.put(item.getSkuSpec(),item.getId());
        });
        productItemVo.setSkuSpecValueMap(specMap);
        // 根据sku对象的productId查询商品基本信息并添加到返回结果集对象中
        Product product = productMapper.selectProductById(productId);
        // 将product对象中的轮番图对象转为集合
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        productItemVo.setProduct(product);
        // 获取图片详情列表
        ProductDetails productDetails = productMapper.selectProductDetailsById(productId);
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        // 获取商品规格信息
        JSONArray specValueList = JSONArray.parseArray(product.getSpecValue());
        productItemVo.setSpecValueList(specValueList);
        return productItemVo;
    }
    @Override
    @Transactional(readOnly = true)
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.selectProductSkuById(skuId);
    }
}
